﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using WebLog;
namespace VotanetWebSite
{
    public partial class MaestraVotante : System.Web.UI.MasterPage
    {
        protected override void OnInit(EventArgs e)
        {
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            JavascriptEnable();
            HttpBrowserCapabilities browser = Request.Browser;
            ValidarBrowser(browser);
            //var ee = Request.UserAgent;
            //var o = browser.Browser;
            //var t = browser.Type;
            //var eee = browser.Version;
            //var eeee = browser.MajorVersion;

            //HtmlGenericControl metatag1 = new HtmlGenericControl("meta");
            //metatag1.Attributes["content"] = "0;url=/Mensajes_Error/JavaScript_Desactivado.aspx";
            //metatag1.Attributes["http-equiv"] = "refresh";
            //Page.Header.Controls.Add(metatag1);
        }

        protected void ValidarBrowser(HttpBrowserCapabilities browser)
        {
            switch (browser.Browser)
            {
                case "Opera":
                    //Aqui es necesario un try .. sorry PPP
                    try
                    {
                        string[] userAgent = Request.UserAgent.Split('/');
                        string[] version = userAgent[3].Split('.');

                        if (Convert.ToInt32(version[0]) < 9)
                        {
                            Response.RedirectPermanent("Mensajes_Error/Navegador_Incompatible.aspx");
                        }
                    }
                    catch (Exception e)
                    {
                        Logger logger = Logger.GetInstance;
                        logger.WriteLog(e);
                        Response.RedirectPermanent("Mensajes_Error/Navegador_Incompatible.aspx");
                    }
                    break;
                case "Chrome":
                    if (browser.MajorVersion < 9)
                    {
                        Response.RedirectPermanent("Mensajes_Error/Navegador_Incompatible.aspx");
                    }
                    break;
                case "Safari":
                    if (browser.MajorVersion < 5)
                    {
                        Response.RedirectPermanent("Mensajes_Error/Navegador_Incompatible.aspx");
                    }
                    break;
                case "Firefox":
                    if (browser.MajorVersion < 4)
                    {
                        Response.RedirectPermanent("Mensajes_Error/Navegador_Incompatible.aspx");
                    }
                    break;
                case "IE":
                    if (browser.MajorVersion < 9)
                    {
                        Response.RedirectPermanent("Mensajes_Error/Navegador_Incompatible.aspx");
                    }
                    break;
            }
        }

        protected void JavascriptEnable()
        {
            HtmlGenericControl ctrl = new HtmlGenericControl("noscript");
            ctrl.InnerHtml = string.Format("<meta http-equiv=REFRESH " +
                             "content=0;URL={0}>", "/Mensajes_Error/JavaScript_Desactivado.aspx");
            Page.Header.Controls.Add(ctrl);
        }
    }
}